package cn.rtangyy.seckilling.service.impl;

import cn.rtangyy.seckilling.dao.SeckillOrderDao;
import cn.rtangyy.seckilling.entity.SeckillOrder;
import cn.rtangyy.seckilling.entity.User;
import cn.rtangyy.seckilling.service.SeckillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class SeckillOrderServiceImpl implements SeckillOrderService {

    @Autowired
    private SeckillOrderDao secKillOrderDao;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public boolean orderIsExist(Long userId, Long goodsId) {
        Integer count = secKillOrderDao.orderIsExist(userId, goodsId);
        return null == count ? false : true;
    }

    @Override
    public void insertSecOrder(SeckillOrder seckillOrder) {
        secKillOrderDao.insertSecOrder(seckillOrder);
    }

    @Override
    public Long getResult(User user, Long goodsId) {
        Long orderId = secKillOrderDao.findOrderId(user.getId(), goodsId);
        if (orderId != null) {
            return orderId;
        }else if (redisTemplate.hasKey("isStockEmpty:"+goodsId)) {
            return -1L;
        }else {
            return 0L;
        }
    }
}
